<template>
  <div class="contentRight2">
    <div id="tools">

      <div class="height8"></div>
      <!-- 查询条件 -->
      <a-form layout="inline" style="text-align: left;padding: 0 10px;" :selfUpdate="true">

        <a-form-item label="选择日期" class="marginBtm">
          <a-config-provider :locale="localezh_CN">
            <a-range-picker @change="onChangePicker" :locale="localezh_CN" :default-value="[startDate, endDate]" format="YYYY-MM-DD HH:mm:ss" :show-time="{ format: 'HH:mm:ss' }"/>
          </a-config-provider>
        </a-form-item>

        <a-form-item label="方向" class="marginBtm">
          <!--99全部1出2进-->
          <a-dropdown style="">
            <a-menu slot="overlay" @click="handleMenuClick">
              <a-menu-item :key="item.id" v-for="item in approvalStatus">{{item.name}}</a-menu-item>
            </a-menu>
            <a-button>{{ statusValue }}
              <a-icon type="down"/>
            </a-button>
          </a-dropdown>
        </a-form-item>

        <a-form-item label="关键字" class="marginBtm">
          <a-input v-model="queryValue" allowClear placeholder="请输入工号/姓名/车牌" class="marginBtm"
                   @pressEnter="inquireStart"/>
        </a-form-item>

        <a-form-item class="marginBtm">
          <a-button type="primary" @click="inquireStart">
            查询
          </a-button>
        </a-form-item>

      </a-form>
      <a-divider style="margin: 5px 0"/>
      <div style="padding: 5px 10px 10px 10px;text-align: left;">
        <a-button type="primary" class="button-marin" @click="exportList">
          导出
        </a-button>
        <!--<a-button type="primary" class="button-marin" @click="addDilog">-->
        <!--新增-->
        <!--</a-button>-->
      </div>
    </div>

    <a-table
      class="tabled"
      bordered
      :fixed="true"
      size="small"
      :columns="columns"
      :row-key="record => Math.random()"
      :data-source="tableData"
      :pagination="pagination"
      :loading="loading"
      :scroll="{ x:xWidth,y: yHeight }"
      :locale="localezh_CN"
      @change="handleTableChange"
    >
      <!--点击行选中行，点击单选按钮选中行；但是需要:row-key是唯一标示字段-->
      <!--:rowSelection="{type:'radio',selectedRowKeys:selectedRowKeys,fixed:true,onChange:onChange}"-->
      <!--:customRow="clickRow"-->

      <!-- <template slot="name" slot-scope="name"> {{ name.first }} {{ name.last }} </template> -->
    </a-table>

    <a-modal v-model="visible" title="新增" @ok="handleOk">
      <p>Some contents...</p>
      <p>Some contents...</p>
      <p>Some contents...</p>
    </a-modal>

  </div>
</template>

<script>
  import {tableBoxHeight} from "@/api/communal";
  import reservationApi from "@/api/reservation";
  import localezh_CN from 'antd/lib/locale-provider/zh_CN';
  import dateUtil from "@/utils/dateUtils";
  import moment from "moment";
  import {ConfigProvider} from 'ant-design-vue';//引用
  import 'moment/locale/zh-cn';
  import cfg from '@/config/config'

  export default {
    components: {
      ConfigProvider
    },
    name: '',
    data() {
      return {
        localezh_CN,
        spinning: false,
        delayTime: 500,
        xWidth: "",
        yHeight: "",
        tableData: [],
        pagination: {
          pageSize: 30,
          current: 1,
          total: 100,
          showSizeChanger: true,
          // showQuickJumper: true,
          pageSizeOptions: ['30', '50', '100'],
          showTotal: (total) => `共 ${total} 条`, // 显示总数
          onShowSizeChange: (current, pageSize) => {
            // console.log("1111");
            this.pagination.current = 1;
            this.pagination.pageSize = pageSize;
          },
        },
        selectedRowKeys: [],
        loading: false,
        dateFormat: 'YYYY/MM/DD',
        startDate: "",
        endDate: "",
        startDated: "",
        endDated: "",
        queryValue: "",  //查询值,访客名称，访客手机号
        columns: [],   //表格表头
        statusValue: "",  //状态名称
        statusId: 99,   //状态id，<!--99全部1出2进-->
        approvalStatus: [
          {name: "全部", id: 99},
          {name: "出", id: 1},
          {name: "进", id: 2},

        ],
        visible: false


      }
    },
    beforeMount() {
      // carDatetime: "2020-11-15 10:11:43"
      // carInout: "进"
      // carName: "沪BDS391"
      // carSbidName: "一期出口左侧"
      // userLname: null
      // userNo: null
      this.columns = [
        {
          title: '序号',     //表头
          dataIndex: 'serialNumber',   //字段名称
          width: '50px',    //设列宽度
          align: 'center',  //居中显示
          className: 'changeBack',   //重置覆盖背景色为浅灰色，否则是白色的
          customRender: (value, row, index) => `${(this.pagination.current - 1) * this.pagination.pageSize + index + 1}`  //设置动态序号
        },
        {
          title: '工号',
          dataIndex: 'userNo',
          width: '80px',
          align: 'center',
          className: 'changeBack'
        },
        {
          title: '姓名',
          dataIndex: 'userLname',
          width: '100px',
          align: 'center',
          className: 'changeBack'
        },
        {
          title: '车牌',
          dataIndex: 'carName',
          width: '100px',
          align: 'center',
          className: 'changeBack'
        },
        {
          title: '道口位置',
          dataIndex: 'carSbidName',
          width: '180px',
          minWidth: '150px',
          align: 'center',
          className: 'changeBack',
          ellipsis: true,   //此属性可以让单元格内容根据宽度自动省略
        },
        {
          title: '方向',
          dataIndex: 'carInout',
          width: '100px',
          align: 'center',
          className: 'changeBack',
        },
        {
          title: '预约开始',
          dataIndex: 'visitDateBegin',
          width: '180px',
          align: 'center',
          className: 'changeBack'
        },
        {
          title: '时间',
          dataIndex: 'carDatetime',
          width: '180px',
          align: 'center',
          className: 'changeBack'
        }
      ];
      // this.tableData =[
      //   {userNo:12213,userLname:"张四",carName:"F12312",carSbidName:"2131",carInout:"进",visitDateBegin:"",carDatetime:""}
      // ]
      this.startDate = moment(dateUtil.getFirstDay(), this.dateFormat);
      this.endDate = moment(dateUtil.getLastDay(), this.dateFormat);
      this.startDated = moment(this.startDate).format('YYYY-MM-DD HH:mm:ss');
      this.endDated = moment(this.endDate).format('YYYY-MM-DD HH:mm:ss');
    },
    mounted() {
      // console.log(tableBoxHeight());
      this.yHeight = tableBoxHeight() + 3  + "px";
      this.xWidth = this.getTableWidth();
      this.statusValue = this.approvalStatus[0].name;
      this.statusId = this.approvalStatus[0].id;
      let minHeight = document.getElementsByClassName("ant-table");
      minHeight[0].style.minHeight = tableBoxHeight() + 40 + "px";

      // this.inquireStart();

    },
    methods: {
      // 计算表格宽度
      getTableWidth() {
        let totals = null;
        var el;
        for (el of this.columns) {
          totals += parseInt(el.width);
        }
        return totals + 100 + "px"
      },
      // 选择日期
      onChangePicker(date, dateString) {
        // console.log(date, dateString);
        this.startDated = dateString[0];
        this.endDated = dateString[1];
      },
      // 导出列表
      exportList() {
        if (this.startDated == "") {
          this.$message.warning("开始日期不能为空");
          return
        }
        if (this.endDated == "") {
          this.$message.warning("结束日期不能为空");
          return
        }

        this.spinning = true;
        let param = {
          current: this.pagination.current,
          pageSize: this.pagination.pageSize,
          startDate: this.startDated,
          endDate: this.endDated,
          carInout: this.statusId,
          serchTxt: this.queryValue
        };
        let jsonIn = encodeURI(encodeURI(JSON.stringify(param)));
        window.open(cfg.apiUrl + 'api/visitor/exportSmartCartempList?jsonIn=' + jsonIn, "_self");
        this.spinning = false;

      },
      // 开始查询
      inquireStart() {
        this.pagination.current = 1;
        let param = {
          current: this.pagination.current,
          pageSize: this.pagination.pageSize,
          startDate: this.startDated,
          endDate: this.endDated,
          carInout: this.statusId,   //carInout
          serchTxt: this.queryValue
        };
        // console.log(param);
        this.fetch(param);
      },
      // 审批状态下拉菜单
      handleMenuClick(e) {
        // console.log(e);
        this.approvalStatus.filter(el => {
          if (el.id == e.key) {
            // console.log(el);
            this.statusValue = el.name;
            this.statusId = el.id
          }
        });
        // console.log(this.statusValue,this.statusId);
      },
      // 点击行
      clickRow(record) {
        return {
          on: {
            click: () => {
              console.log(record)
              let keys = [];
              keys.push(record.orderId);
              this.selectedRowKeys = keys;
            }
          }
        }

      },
      // 表格切换分页
      handleTableChange(pagination, filters, sorter) {
        // console.log(pagination);
        const pager = {...this.pagination};
        pager.current = pagination.current;
        this.pagination = pager;
        this.fetch({
          pageSize: pagination.pageSize,
          current: pagination.current,
          startDate: this.startDated,
          endDate: this.endDated,
          approvalStatus: this.statusId,
          serchTxt: this.queryValue,
          sortField: sorter.field,
          sortOrder: sorter.order,
          ...filters,
        });
      },
      addDilog() {
        this.visible = true;
      },
      handleOk() {
        console.log("点击了ok");
        this.visible = false;
      },

      onChange(selectedRowKeys, selectedRows) {
        // console.log(selectedRowKeys, selectedRows);
        this.selectedRowKeys = selectedRowKeys;
      },
      // 请求表格数据--测试用
      async fetch(params) {
        this.loading = true;
        let resp = await reservationApi.findSmartCartempList(params);
        // console.log(resp.data);
        if (resp.data.success) {
          // console.log("111");
          this.loading = false;
          this.pagination.total = resp.data.result.total;
          this.tableData = resp.data.result.records;

        } else {
          // console.log("222");
          this.loading = false;
          this.$message.error(resp.data.message);
        }

        // 测试
        // setTimeout(() => {
        //   this.tableData=[
        //     {serialNumber:"0",oddNumber:"14",visitName:"王晓明",visitCompony:"上海步达",visitTel:"13345674568",vistorCertNumber:"410526194601114235",userLname:"冯伟",userTelephone:"17612345678",visitDateBegin:"2020-10-10 08:00:00",visitDateEnd:"2020-10-10 17:30:00",entourage:"主访客",approvalStatus:"已通过"},
        //     {serialNumber:"0",oddNumber:"14",visitName:"王晓明",visitCompony:"上海步达",visitTel:"13345674568",vistorCertNumber:"410526194601114235",userLname:"冯伟",userTelephone:"17612345678",visitDateBegin:"2020-10-10 08:00:00",visitDateEnd:"2020-10-10 17:30:00",entourage:"主访客",approvalStatus:"已通过"},
        //   ];
        // this.tableData=this.tableData.map((el,i)=>{
        //    el.serialNumber = i + 1;
        //    return el
        // });
        // console.log(this.tableData);
        //   this.loading = false;
        // }, 2000);
      },


    },
  }
</script>

<style scoped>

</style>

